import axios from 'axios';

import {Loading, Message, MessageBox} from 'element-ui'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let request = axios.create({
    baseURL: "http://localhost:8848/api",
    timeout: 30000,
    withCredentials: false
});
let loadingInstance;
request.interceptors.request.use(config => {
    loadingInstance = Loading.service({
        text: "努力加载中···",
        background: "transparent",
    });

    //在请求头加入token，名字要和后端接收请求头的token名字一样
    config.headers.token = sessionStorage.getItem("token");

    return config;
}, error => {
    // 对请求错误做些什么
    Message.error({
        message: "请求错误！"
    });
    return Promise.reject(error);
});
// 添加响应拦截器
request.interceptors.response.use(response => {
    // 对响应数据做点什么
    loadingInstance.$nextTick(() => {
        loadingInstance.close();
    });
    return response;
}, error => {
    // 对响应错误做点什么
    loadingInstance.$nextTick(() => {
        loadingInstance.close();
    });
    Message.error({
        message: "服务器未响应！"
    });
    return Promise.reject(error);
});

export default request;